package com.jibei.mendo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.blankj.utilcode.util.FileUtils;
import com.jibei.mendo.R;
import com.jibei.mendo.adapter.FileAdapter;
import com.jibei.mendo.adapter.FileDateAdapter;
import com.jibei.mendo.event.Event;
import com.jibei.mendo.utils.Constant;
import com.jibei.mendo.utils.ExcelUtil;
import com.jibei.mendo.utils.VibratorUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by lhl on 2019/8/17.
 */
public class FileActivity extends BaseActivity implements FileAdapter.ReadFileListener, View.OnClickListener {

    private ImageView iv_back;

    private RecyclerView rv_file_name;
    private List<String> strings = new ArrayList<>();
    private FileAdapter mFileAdapter;

//    private RecyclerView rv_date;
    private List<String> dateStrings = new ArrayList<>();
//    private FileDateAdapter mFileDateAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);

        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        rv_file_name = findViewById(R.id.rv_file_name);
        rv_file_name.setLayoutManager(new LinearLayoutManager(this));
        mFileAdapter = new FileAdapter(this, strings, dateStrings,this);
        rv_file_name.setAdapter(mFileAdapter);

       /* rv_date = findViewById(R.id.rv_date);
        rv_date.setLayoutManager(new LinearLayoutManager(this));
        mFileDateAdapter = new FileDateAdapter(this, dateStrings);
        rv_date.setAdapter(mFileDateAdapter);*/

        initData();
    }

    private void initData() {
        List<File> fileList = FileUtils.listFilesInDir(ExcelUtil.getInstance().getExcelPath());
        if (fileList != null && fileList.size() > 0) {
            for (File file : fileList) {
                String excelPath = file.getAbsolutePath();
                if (excelPath != null) {
                    strings.add(file.getName());

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                    dateStrings.add(sdf.format(FileUtils.getFileLastModified(excelPath)));
                }
            }
        }
        mFileAdapter.notifyDataSetChanged();
//        mFileDateAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onActivityReceiveEvents(Event event) {
        boolean isCaptured = false;//子Activity是否捕获事件
        if (super.onActivityReceiveEvents(event)) {
            return isCaptured;
        }
        isCaptured = true;
        switch (event.getTag()) {
            case Constant.Events.DIAN_HUO:
            case Constant.Events.JIAN_CE:
            case Constant.Events.KONG_DANG:
            case Constant.Events.READY_PRESS:
            case Constant.Events.DIAN_HUO_PRESS:
                finish();
                break;
            default:
                isCaptured = false;
                break;
        }
        return isCaptured;
    }

    @Override
    public void onClick(View v) {
        VibratorUtil.getInstance().executeVibrator(this, 100);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onReadFileListener(View mView, ProgressBar mProgressBar, int position) {

    }
}
